// SortOrderSetter.m

#import <eointerface/eointerface.h>
#import "SortOrderSetter.h"
#import "TableMagic.h"

@implementation SortOrderSetter
- (NSArray *)orderingArrayForEntity:(EOEntity *)entity keys:(NSArray *)keys
                 ordering:(EOOrdering)ordering
{
    int count, i;
    NSMutableArray *orderarray;
    
    count = [keys count];
    orderarray = [NSMutableArray arrayWithCapacity:count];
    
    for(i=0; i<count; i++) {
        EOAttribute *attr = [entity attributeNamed:[keys objectAtIndex:i]];
        if (attr)
            [orderarray addObject: [EOAttributeOrdering attributeOrderingWithAttribute:attr
		ordering:ordering]];
    }
    return orderarray;
}

- applyOrderingToDataSourceAndFetch:(EOOrdering)ordering
{
    id tableView = [NXTableView keyTableView];
    EOController *controller = [tableView eoController];
    id dataSource = [controller dataSource];
    if (controller && [dataSource respondsToSelector:@selector(setFetchOrder:)]) {
        NSArray *keys = [tableView associationKeysForSelectedColumns];
        if (!keys) // if none selected, sort by first
            keys = [NSArray arrayWithObject:[tableView associationKeyForColumn:0]];
        [dataSource setFetchOrder:[self orderingArrayForEntity:[dataSource entity] keys:keys ordering:ordering]];

        // nil out the fetch order on the controller (since it will override this one)
        [controller setSortOrdering:nil];
        
        [controller fetch:nil];
        return self;
    }
    
    NXBeep();
    return nil;
}

- sortDataSourceAscending:sender
{
    return [self applyOrderingToDataSourceAndFetch:EOAscendingOrder];
}

- sortDataSourceDescending:sender
{
    return [self applyOrderingToDataSourceAndFetch:EODescendingOrder];
}

- (NSArray *)keyOrderingForKeys:(NSArray *)keys ordering:(NSComparisonResult)ordering
{
    int i, count = [keys count];
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
    for(i=0; i<count; i++) {
        [result addObject:[EOKeySortOrdering keyOrderingWithKey:[keys objectAtIndex:i] ordering:ordering]];
    }
    return result;
}

- applyOrderingToControllerAndFetch:(NSComparisonResult)ordering
{
    id tableView = [NXTableView keyTableView];
    EOController *controller = [tableView eoController];
    if (controller) {
        NSArray *keys = [tableView associationKeysForSelectedColumns];
        if (!keys) // if none selected, sort by first
            keys = [NSArray arrayWithObject:[tableView associationKeyForColumn:0]];
        [controller setSortOrdering:[self keyOrderingForKeys:keys ordering:ordering]];
        [controller resort];

        if ([[controller dataSource] respondsToSelector:@selector(setFetchOrder:)]) {
            // unset the fetch order on the dataSource
            [(EODatabaseDataSource *)[controller dataSource] setFetchOrder:nil];
        }
        return self;
    }

    NXBeep();
    return nil;
    
}

- sortControllerAscending:sender
{
    return [self applyOrderingToControllerAndFetch:NSOrderedAscending];
}

- sortControllerDescending:sender
{
    return [self applyOrderingToControllerAndFetch:NSOrderedDescending];
}


// switch old style methods to use controller sorting
- setOrderAscending:sender
{
    return [self sortControllerAscending:sender];
}

- setOrderDescending:sender
{
    return [self sortControllerDescending:sender];
}
@end
